<?php
namespace app\manage\controller;

use app\manage\controller\Base;
use app\model\ImFriendModel;
use app\model\ImGroupModel;
use app\model\ImGroupUserModel;
use app\model\ImMessageFriendModel;
use app\model\ImMessageGroupModel;
use think\facade\Db;
use support\View;
use support\Request;
use app\model\UserModel;

class ImGroupController extends Base
{
    public function index(Request $request)
    {

        $key=$request->input('key','');
        View::assign('key', $key);
        $order='id desc';
        $user=ImGroupModel::where('title', 'like', '%'.$key.'%')
            ->order($order)->paginate(['list_rows'=>10,'query'=>request()->all()])->each(function ($item, $key) {

            });
        View::assign('user', $user);
        View::assign('count1', $user->total());
        return view('im_group/index');
    }

    public function add(Request $request)
    {
        if (request()->method()=='POST') {
            $data=$request->post();
            $file = $request->file('pic');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/im/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['pic']=$path;
				$arr=[
                    'title'=>$data['title'],
                    'img'=>$data['pic'],
                ];
            }else{
                $arr=[
                    'title'=>$data['title']
                ];
            }
            ImGroupModel::where('id', $u->id)->update(['group_no'=>$this->sui($u->id)]);
            return view_s('添加成功');
        }
        return view('im_group/add');
    }
    public function edit(Request $request)
    {
        if (request()->method()=='POST') {
            $data=$request->post();
            $file = $request->file('pic');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/im/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['pic']=$path;
                $arr=[
                    'title'=>$data['title'],
                    'img'=>$data['pic'],
                ];
            }else{
                $arr=[
                    'title'=>$data['title']
                ];
            }
            ImGroupModel::where('id',$data['id'])->update($arr);
            return view_s('修改成功');
        }
        $cid=$request->get('id',0);
        $data=ImGroupModel::where('id', $cid)->find();
        View::assign('data', $data);
        return view('im_group/edit');
    }

    public function ajax(Request $request)
    {
        $data=$request->all();
        if ($data['type']=='del') {
            if (ImGroupModel::where('id', $data['id'])->update(['delete_time'=> time()])) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        return json(['code'=>0,'msg'=>'失败']);
    }








    public function friend(Request $request)
    {
        $key=$request->get('key', '');
        $id=$request->get('id', 0);
        View::assign('key', $key);
        $user=ImGroupUserModel::alias('igu')
            ->join('user u', 'u.id=igu.user_id')
            ->field('igu.*,u.name,u.phone,u.img as avatarUrl,u.sex as gender')
            ->where('igu.group_id', $id)
            ->where('u.name|u.phone', 'like', '%'.$key.'%')
            ->order('igu.id desc')->paginate(['list_rows'=>10,'query'=>request()->all()]);
        View::assign('user', $user);
        View::assign('count1', $user->total());
        return view('im_group/friend');
    }
    public function show(Request $request)
    {
        $key=$request->get('key', '');
        View::assign('key', $key);
        $id=$request->get('id', 0);
        View::assign('id', $id);
        $user=ImMessageGroupModel::alias('m')
            ->leftJoin('im_message_type mt','m.message_type=mt.id')
            ->leftJoin('user u','m.user_id=u.id')
           ->where('m.to_group_id',$id)
            ->where('m.content', 'like', '%'.$key.'%')
            ->order('m.id desc')
            ->field('m.id,m.content,m.create_time,m.extra_info,m.to_group_id as to_id,m.user_id,mt.name as msgType,u.img as toFace,u.name as toName')
            ->paginate(['list_rows'=>10,'query'=>request()->all()]);


        View::assign('user', $user);
        View::assign('count1', $user->total());
        return view('im_group/show');
    }

    private function sui($id=0)
    {
        $str=[
            0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
        ];
        $sui='';
        for ($i=0;$i<5;$i++) {
            $sui.=$str[mt_rand(0, 35)];
        }

        return $sui.$id;
    }
}
